﻿@page "/Admin/Menus"
@attribute [Authorize]
@attribute [TabItemOption(Text ="菜单管理",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysAppService SysAppService
@inject ISysMenuService SysMenuService

<div class="row h-100 overflow-hidden">
    <div class="col-2 h-100">
        <Card class="h-100">
            <CardHeader>应用名称</CardHeader>
            <CardBody>
                <AsyncRender OnQueryAsync="SysAppService.GetSelectedAppsAsync" OnAfterQueryAsync="OnAfterQueryAsync">
                    <RadioList Items="context" @bind-Value="@_selectApp" OnSelectedChanged="OnSelectedAppChanged" IsVertical="true"></RadioList>
                </AsyncRender>
            </CardBody>
        </Card>
    </div>

    <div class="col-10 h-100">
        <Table TItem="MenuOutput"
                @ref = "@_table"
                IsTree="true"
                IsPagination="false"
                IsStriped="true"
                IsMultipleSelect="true"
                IsFixedHeader="true" 
                AllowResizing="true"
                ShowLoading="true"
                ShowColumnList="true"
                ShowCardView="true"
                ShowToolbar="true"
                ShowSearch="true"
                ShowExtendButtons="true" 
                ShowEmpty="true" 
                SearchMode="SearchMode.Top"
                SearchModel="@_search"
                EditDialogItemsPerRow="1"
                EditDialogSize="Size.Large"
                PageItemsSource="@(new int[] { 15, 50 })"
                HasChildrenCallback="@((x)=>x.Children.Any())"
                OnQueryAsync="@OnQueryAsync"
                OnTreeExpand="@OnTreeExpand"
                OnSaveAsync="@OnSaveAsync">
            <TableColumns>
                <TableColumn @bind-Field="@context.Name" />
                <TableColumn @bind-Field="@context.Type" Width="80"/>
                <TableColumn @bind-Field="@context.Icon" Width="80">
                    <Template Context="val">
                        <i class="fa @val.Value"></i>
                    </Template>
                </TableColumn>
                <TableColumn @bind-Field="@context.Component" />
                <TableColumn @bind-Field="@context.Router" />
                <TableColumn @bind-Field="@context.Sort" Width="80"/>
            </TableColumns>
            <EditTemplate>
                <div class="row g-3 form-inline">
                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.Name" maxlength="50" />
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.Code" maxlength="50" />
                    </div>

                    <div class="col-sm-6">
                        <Select @bind-Value="@context.Application" Items="@_apps" 
                            OnSelectedItemChanged="((v)=> context.ChangedAsync(()=>context.Application))"></Select>
                    </div>

                    <div class="col-sm-6">
                        <RadioList class="item-w0" @bind-Value="@context.Type" 
                            OnSelectedChanged="((vs,v) => context.ChangedAsync())"></RadioList>
                    </div>

                    <ConditionRender Watch="@context" Condition="true">
                        <div class="col-sm-6">
                            <ConditionRender Condition="@(context.Type == MenuType.DIR)">
                                <TrueContent>
                                    <BootstrapInput @bind-Value="@context.Redirect" maxlength="50" placeholder="请输入重定向地址" />
                                </TrueContent>
                                <FalseContent>
                                    <AsyncRender Context="val"
                                        TValue="IEnumerable<CascaderItem>" 
                                        TriggerValue ="@context.Application"
                                        OnQueryAsync="(()=> SysMenuService.GetCascaderMenusAsync(context.Application??_apps?.FirstOrDefault()?.Value))" >
                                        <LoadingContent>
                                            <BootstrapInputLocker @bind-Value="@context.Pid" Prompt="数据加载中..."/>
                                        </LoadingContent>
                                        <ChildContent>
                                            <Cascader @bind-Value="@context.Pid" Items="val"></Cascader>
                                        </ChildContent>
                                    </AsyncRender>
                                </FalseContent>
                            </ConditionRender>
                            </div>

                        <div class="col-sm-6">
                            <RadioList class="item-w0"
                                @bind-Value="@context.OpenType"
                                IsDisabled="@(context.Type == MenuType.BTN)"
                                OnSelectedChanged="((vs,v) => context.ChangedAsync())"></RadioList>
                        </div>

                        <Divider></Divider>

                        <ConditionRender Condition="@(context.Type == MenuType.DIR)">
                            <div class="col-sm-6">
                            <BootstrapInput maxlength="50" 
                                @bind-Value="@context.Component" 
                                IsDisabled="@(context.Type == MenuType.BTN || context.Type == MenuType.DIR||(context.Type == MenuType.MENU && context.OpenType == MenuOpenType.OUTER))"
                                SkipValidate="@(context.OpenType == MenuOpenType.OUTER)" />
                            </div>
                        </ConditionRender>

                        <ConditionRender Condition="@(context.Type == MenuType.BTN)">
                            <TrueContent>
                                <div class="col-sm-6">
                                    <BootstrapInput @bind-Value="@context.Permission" PlaceHolder="请输入权限标识" maxlength="50" />
                                </div>
                            </TrueContent>
                            <FalseContent>
                                <div class="col-sm-6">
                                    <BootstrapInput @bind-Value="@context.Router" maxlength="50" />
                                </div>

                                <ConditionRender Condition="@(context.OpenType == MenuOpenType.OUTER || context.OpenType == MenuOpenType.INNER)">
                                    <div class="col-sm-6">
                                        <BootstrapInput maxlength="50"
                                            @bind-Value="@context.Link" 
                                            IsDisabled="@(context.OpenType == MenuOpenType.NONE || context.OpenType == MenuOpenType.COMPONENT)" 
                                            SkipValidate="@(context.OpenType == MenuOpenType.NONE || context.OpenType == MenuOpenType.COMPONENT)" 
                                            PlaceHolder="请输入内外链打开地址" />
                                    </div>
                                </ConditionRender>

                                <div class="col-sm-6">
                                    <IconPicker @bind-Value="@context.Icon" Items="@_icons" PlaceHolder="请点击选择图标" ShowSearch="true"/>
                                </div>                    
                            </FalseContent>
                        </ConditionRender>
                    </ConditionRender>

                    <div class="col-sm-6">
                        <RadioList @bind-Value="@context.Weight" class="item-w0"></RadioList>
                    </div>

                    <div class="col-sm-6">
                        <RadioList TValue="string" @bind-Value="@context.Visible" Items="_visibles" class="item-w0"></RadioList>
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInputNumber @bind-Value="@context.Sort" ShowButton="true" Max="999" Min="0" Step="5" />
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.Remark" maxlength="50" placeholder="请输入备注" />
                    </div>
                </div>
            </EditTemplate>
            <SearchTemplate>
                <GroupBox Title="搜索条件">
                    <div class="row g-3 form-inline">
                        <div class="col-12 col-sm-6">
                            <BootstrapInput @bind-Value="@context.Name" ShowLabel="true"/>
                        </div>
                    </div>
                </GroupBox>
            </SearchTemplate>
        </Table>
    </div>
</div>

@code{
    private Table<MenuOutput>? _table;

    private MenuOutput _search = new();
    private string? _selectApp;
    private IEnumerable<SelectedItem>? _apps;
    private IEnumerable<SelectedItem>? _visibles;
    private IEnumerable<SelectedItem>? _icons;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        
        _visibles = typeof(ShowOrHide).ToSelectList();
        _icons = typeof(FontAwesome).ToDisplaySelectList();
    }

    private async Task OnAfterQueryAsync(IEnumerable<SelectedItem> context)
    {
        _apps = context;
        _selectApp = _apps?.FirstOrDefault()?.Value;
        await _table!.QueryAsync();
    }

    private Task OnSelectedAppChanged(IEnumerable<SelectedItem> values, string? val)
        => _table!.QueryAsync();

    private Task<QueryData<MenuOutput>> OnQueryAsync(QueryPageOptions options)
    {
        //没选择应用时不查询数据
        if (string.IsNullOrWhiteSpace(_selectApp))
        {
            return Task.FromResult(new QueryData<MenuOutput>());
        }

        var search = _search.Adapt<GetMenuListInput>();
        search.Application = _selectApp;
        return SysMenuService.ListAsync(search);
    }

    private Task<IEnumerable<MenuOutput>> OnTreeExpand(MenuOutput item)
        => Task.FromResult(item.Children.AsEnumerable());

    private Task<bool> OnDeleteAsync(IEnumerable<MenuOutput> items)
        => SysMenuService.DeleteAsync(items);

    private async Task<bool> OnSaveAsync(MenuOutput item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysMenuService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysMenuService.EditAsync(item);
                break;
        }
        return result;
    }
}